//
//  CWSportSpeaker.m
//  大神专制黑马行
//
//  Created by chenWei on 2017/5/17.
//  Copyright © 2017年 陈伟. All rights reserved.
//

#import "CWSportSpeaker.h"
#import <AVFoundation/AVFoundation.h>


@interface CWSportSpeaker ()
//播报运动类型
@property(nonatomic, assign) SportType sportType;
//播报运动类型字符串
@property(nonatomic, copy) NSString *sportTypeStr;

//【已经】播报的总单位距离
@property(nonatomic, assign) int totalDistanceUnit;

//语言合成器
@property(nonatomic, strong) AVSpeechSynthesizer *speechSynthesizer;

@end

@implementation CWSportSpeaker

- (instancetype)initWithDistanceUnit:(int)distanceUnit SportType:(SportType)sportType
{
    self.distanceUnit = distanceUnit;
    self.sportType = sportType;
    self.speechSynthesizer = [[AVSpeechSynthesizer alloc]init];
    
    return self;
}



/**
 运动开始播报
 */
- (void)sportStart
{
    NSString *typeStr;
    
    switch (self.sportType) {
        case SportTypeRun:
            typeStr = @"跑步";
            break;
        case SportTypeWalk:
            typeStr = @"走路";
            break;
        case SportTypeRide:
            typeStr = @"骑行";
            break;
            
        default:
            break;
    }

    //保存类型文本
    self.sportTypeStr = typeStr;
    
    //播报语音文本
    [self playVoiceText:[NSString stringWithFormat:@"%@",self.sportTypeStr]];
    
}


/**
 运动状态改变播报
 
 @param sportState 运动状态
 */
- (void)sportStateChange:(SportState)sportState
{
    NSString *stateStr;
    
    switch (sportState) {
        case SportStatePause:
            stateStr = @"运动已暂停";
            break;
        case SportStateContinue:
            stateStr = @"运动已恢复";
            break;
        case SportStateStop:
            stateStr = @"休息一下吧";
            break;
            
        default:
            break;
    }
    
    [self playVoiceText:stateStr];

}



/**
 整点距离，时间，速度播报
 
 @param distance 距离
 @param totalTime 时间
 @param avgSpeed 速度
 */
- (void)sportWithDistance:(CGFloat)distance TotalTime:(CGFloat)totalTime AvgSpeed:(CGFloat)avgSpeed
{   //如果当前距离大于 上次播报的总距离加单位播报距离
    if (distance > self.totalDistanceUnit + self.distanceUnit) {
        self.totalDistanceUnit += self.distanceUnit;
        
        //创建播放文本
        NSString *playStr = [NSString stringWithFormat:@"您已经%@ %d公里  用时%@ 平均速度%0.2f公里/小时",self.sportTypeStr,(int)distance,[self tansferForPlayTextWithTotalTime:totalTime],avgSpeed];
     
        //播报语音文本
        [self playVoiceText:playStr];
    }
}

//将时间转化为语音播报文字
- (NSString *)tansferForPlayTextWithTotalTime:(CGFloat)totalTime
{
    int hour = totalTime/3600;
    int minute = (int)totalTime%3600/60;
    int second = (int)totalTime%60;

    NSString *hourStr;
    
    if (hour > 0) {
        hourStr = [NSString stringWithFormat:@"%d小时",hour];
    }else {
        hourStr = @"";
    }
    
    NSString *minuteStr;
    if ((minute == 0) && (hour == 0)) {
        minuteStr = @"";
    }
    else
    {
        minuteStr = [NSString stringWithFormat:@"%d分",minute];
    }

    NSString *secondStr;
    if (second == 0) {
        secondStr = @"";
    }
    else
    {
        secondStr = [NSString stringWithFormat:@"%d秒",second];
    }
    
    return [NSString stringWithFormat:@"%@ %@ %@",hourStr,minuteStr,secondStr];

}



#pragma mark - 把文本转换成语音
- (void)playVoiceText:(NSString *)sportTypeStr
{
    //1.创建语音合成器
    //AVSynchronizedLayer *synchronizedLayer = [[AVSynchronizedLayer alloc]init];
    
    //当播报语音时，如果上一条语音未播报完毕，则会线程延迟，导致无法播放，播报之前需要判断上一条语音是否播报完毕，如果没有播报完毕就结束上一条
    if (self.speechSynthesizer.isSpeaking == YES) {
        //结束播报
        /**
         AVSpeechBoundaryImmediate,立即停止
         AVSpeechBoundaryWord，等待上一条播报完毕
         */
        [self.speechSynthesizer stopSpeakingAtBoundary:AVSpeechBoundaryImmediate];
        
    }
    
    
    //2.创建要播报的语言， 参数：要播报的文本  默认播报语言是系统语言
    AVSpeechUtterance *speechUtterance = [[AVSpeechUtterance alloc]initWithString:sportTypeStr];
    
    //3.创建语种
    AVSpeechSynthesisVoice *voice = [AVSpeechSynthesisVoice voiceWithLanguage:@"Zh_Hans"];
    speechUtterance.voice = voice;
    
    //    utterance.volume=1.0;  //设置音量（0.0~1.0）默认为1.0
    //    utterance.rate=0.4;  //设置语速
    //utterance.pitchMultiplier=0.5;  //设置语调  [0.5 - 2]

    
    //4.播放语音
    [self.speechSynthesizer speakUtterance:speechUtterance];

    
}





@end
